/**
 * Created by xc on 15/12/22.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbWechatUserModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //

};
ItbWechatUserModel.prototype = new ItbBaseModel();

// 取得表名
ItbWechatUserModel.prototype.getTableName = function(){
    return 'itb_wechat_user';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbWechatUserModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
}

// 新增订阅用户数据
// 参数: app_id, open_id, union_id
ItbWechatUserModel.prototype.subscribe = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbWechatUserModel.subscribe Err:'+err);
        }
        // Use the connection
        var sql =
            " INSERT INTO itb_wechat_user " +
            " (app_id, open_id, union_id, subscribe_time, unsubscribe_time " +
            " ,create_time, update_time, del_flg) " +
            " VALUES " +
            " (?, ?, ?, sysdate(), null " +
            " , sysdate(), sysdate(), 0)";
        var sqlValue = [ THIS.app_id, THIS.open_id, THIS.union_id ];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            if (error)
                console.log(error);

            callback(error, result.insertId);
        });
    });
}

// 更新取消订阅的用户数据
// 参数: app_id, union_id(或open_id)
ItbWechatUserModel.prototype.unsubscribe = function(callback){
    var THIS = this;

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbWechatUserModel.unsubscribe Err:'+err);
        }
        // Use the connection
        var sql = " UPDATE itb_wechat_user SET unsubscribe_time=sysdate(),update_time=sysdate(),del_flg=1 ";
        if (THIS.union_id && THIS.union_id.length>0){
            sql += " WHERE app_id=? AND union_id=? AND del_flg=0 ";
            var sqlValue = [THIS.app_id, THIS.union_id];
            sql = mysql.format(sql, sqlValue);
        } else if (THIS.open_id && THIS.open_id.length>0){
            sql += " WHERE app_id=? AND open_id=? AND del_flg=0 ";
            var sqlValue = [THIS.app_id, THIS.open_id];
            sql = mysql.format(sql, sqlValue);
        } else {
            return callback("参数错误");
        }

        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            if (error)
                console.log(error);

            callback(error, result);
        });
    });
}

// 根据UnionId查找记录
// 参数：union_id, del_flg
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbWechatUserModel.prototype.findByUnionId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbWechatUserModel.findRecordsByUnionId Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT * FROM itb_wechat_user t ' +
            ' WHERE t.union_id = ? AND t.del_flg = ? '  +
            ' ORDER BY t.update_time DESC ';
        var sqlValue = [THIS.union_id, THIS.del_flg];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}

// 根据UnionId查找记录
// 参数：open_id, del_flg
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbWechatUserModel.prototype.findByOpenId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbWechatUserModel.findRecordsByOpenId Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT * FROM itb_wechat_user t ' +
            ' WHERE t.open_id = ? AND t.del_flg = ? '  +
            ' ORDER BY t.update_time DESC ';
        var sqlValue = [THIS.open_id, THIS.del_flg];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}

// 导出
module.exports = ItbWechatUserModel;